#include "platform.h"
#include "event.h"
#include "applicationengine.h"
using namespace NITK;

static Platform *inst = 0;

Platform::Platform(int &argc, char **argv)
    : data(0)
{
    assert(!inst);
    inst = this;
    init(argc, argv);
    assert(data);
}

Platform * Platform::instance()
{
    return inst;
}

bool Platform::sendEvent(Event *e)
{
    assert(data);
    if (data->applicationEngine) {
        const PositionEvent *pe = static_cast<const PositionEvent*>(e);
        switch (e->type) {
        case Event::Press:
            return data->applicationEngine->handlePress(pe->position);
        case Event::Drag:
            return data->applicationEngine->handleDrag(pe->position);
        case Event::Release:
            return data->applicationEngine->handleRelease(pe->position);
        case Event::WindowGeometryChange:
            data->applicationEngine->handleWindowResize(static_cast<GeometryEvent*>(e)->rectangle);
            return true;
        }
    }
    return false;
}


void Platform::setApplicationEngine(ApplicationEngine *engine)
{
    assert(data);
    data->applicationEngine = engine;
}

ApplicationEngine * Platform::applicationEngine()
{
    assert(data);
    return data->applicationEngine;
}
